package com.lxd.sunso.product.service.impl;

import com.lxd.sunso.common.utils.DateUnit;
import com.lxd.sunso.common.utils.PageUtils;
import com.lxd.sunso.common.utils.Query;
import com.lxd.sunso.product.entity.DatatypeEntity;
import com.lxd.sunso.product.entity.MachineEntity;
import com.lxd.sunso.product.service.DatatypeService;
import com.lxd.sunso.product.service.MachineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxd.sunso.product.dao.DatatemplateDao;
import com.lxd.sunso.product.entity.DatatemplateEntity;
import com.lxd.sunso.product.service.DatatemplateService;


@Service("datatemplateService")
public class DatatemplateServiceImpl extends ServiceImpl<DatatemplateDao, DatatemplateEntity> implements DatatemplateService {

    @Autowired
    DatatypeService datatypeService;

    @Autowired
    MachineService machineService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DatatemplateEntity> page = this.page(
                new Query<DatatemplateEntity>().getPage(params),
                new QueryWrapper<DatatemplateEntity>()
        );
        List<DatatemplateEntity> collect = getDatatemplateEntities(page.getRecords());

        page.setRecords(collect);

        return new PageUtils(page);
    }

    @Override
    public List<DatatemplateEntity> search(Long templateId) {
        List<DatatemplateEntity> datatemplateEntities = this.list(new QueryWrapper<DatatemplateEntity>().eq("TemplateID", templateId));
        List<DatatemplateEntity> collect = getDatatemplateEntities(datatemplateEntities);
        return collect;
    }

    @Override
    public DatatemplateEntity findById(Long id) {
        DatatemplateEntity datatemplateEntity = this.getById(id);
        DatatypeEntity datatypeEntity = datatypeService.getById(datatemplateEntity.getDatatypeid());
        datatemplateEntity.setDatatypeName(datatemplateEntity.getDatatypeName());
        return datatemplateEntity;
    }

    @Override
    public void newupdate(DatatemplateEntity datatemplate) {
        Long datatype = datatemplate.getDatatypeid();
        DatatypeEntity datatypeEntity = datatypeService.getOne(new QueryWrapper<DatatypeEntity>().eq("DataTypeName", datatype));
        if (datatypeEntity == null) {
            this.updateById(datatemplate);
        } else {
            datatemplate.setDatatypeid(datatypeEntity.getId());
            this.updateById(datatemplate);
        }
    }

    @Override
    public boolean savedatatemplate(DatatemplateEntity datatemplate) {
        Long templateid = datatemplate.getTemplateid();
        List<MachineEntity> machineList = machineService.list(new QueryWrapper<MachineEntity>().eq("TemplateID", templateid));
        if (machineList == null || machineList.size() == 0) {
            datatemplate.setCreatetime(DateUnit.dateFormat(new Date()));
            this.save(datatemplate);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean removedatatemplate(List<Long> asList) {
        Long datatemplateId = asList.get(0);
        DatatemplateEntity entity = this.getById(datatemplateId);
        Long templateid = entity.getTemplateid();

        List<MachineEntity> machineList = machineService.list(new QueryWrapper<MachineEntity>().eq("TemplateID", templateid));
        if (machineList == null || machineList.size() == 0) {
            this.removeById(datatemplateId);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public List<String> getControlNames(Long templateId) {
        List<String> controleNames = this.baseMapper.getControlNames(templateId);
        return controleNames;
    }

    @Override
    public List<String> getHeadByMachineName(String machineName) {
        MachineEntity machineEntity = machineService.getOne(new QueryWrapper<MachineEntity>().eq("MachineName", machineName));
        List<DatatemplateEntity> list = this.list(new QueryWrapper<DatatemplateEntity>().eq("TemplateID", machineEntity.getTemplateid()));
        List<String> collect = list.stream().map(entity -> {
            String controlname = entity.getControlname();
            return controlname;
        }).collect(Collectors.toList());
        return collect;
    }

    private List<DatatemplateEntity> getDatatemplateEntities(List<DatatemplateEntity> datatemplateEntities) {
        return datatemplateEntities.stream().map(datatemplateEntity -> {
            DatatypeEntity datatypeEntity = datatypeService.getById(datatemplateEntity.getDatatypeid());
            datatemplateEntity.setDatatypeName(datatypeEntity.getDatatypename());
            return datatemplateEntity;
        }).collect(Collectors.toList());
    }
}